<template>
  <div id="personal-center">
    <h1>PersonalCenter组件</h1>
    <div v-if="account">当前用户：{{account}}</div>
    <mt-button type="primary" @click="logout">注销</mt-button>
  </div>
</template>
<script>
import "./index.less";
import { middleVue, userData } from "../../services";
export default {
  data() {
    return {
      account: ""
    };
  },
  methods: {
    logout() {
      if (confirm("是否注销")) {
        middleVue.$emit("getTotalQuantity", 0);
        userData.removeUserData("zhouqing");
        this.$router.push({
          name: "home"
        });
      }
    }
  },
  beforeRouteEnter(to, from, next) {
    const account = userData.getUserData();
    if (account) {
      next();
    } else {
      middleVue.$emit("openLoginRegist", true);
      next(false);
    }
  },
  created() {
    if (userData.getUserData("zhouqing")) {
      this.account = userData.getUserData("zhouqing");
    }
  }
};
</script>
